package com.lebang.common.util;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.OutputStream;
import java.nio.file.Path;
import java.util.HashMap;

/**
 * @Auther: tony
 * @Date: 2018-11-29 16:37
 * @Description: 二维码的生成解析工具
 */
public class QRCodeUtil {

    private static final Logger log = LoggerFactory.getLogger(QRCodeUtil.class);
    /**
     * 生成二维码
     *
     * @param url      二维码对应的连接地址              eg:https://www.cnblogs.com/whiteme/p/7196582.html
     * @param format   二维码生成图片对应的格式          eg:png
     * @param filePath 二维码生成存储的本地文件路径      eg:E:\test\test\1.png
     */
    public static void create(String url, String format, String filePath) {
        int width = 300;
        int height = 300;
        HashMap map = new HashMap();
        map.put(EncodeHintType.CHARACTER_SET, "utf-8");
        map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        map.put(EncodeHintType.MARGIN, 0);
        try {
            BitMatrix bm = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, width, height);
            Path file = new File(filePath).toPath();
            MatrixToImageWriter.writeToPath(bm, format, file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据二维码的图片，解析二维码的内容
     *
     * @param filePath
     */
    public static void analysis(String filePath) {
        try {
            MultiFormatReader reader = new MultiFormatReader();
            //需要详细了解MultiFormatReader的小伙伴可以点我一下官方去看文档
            File f = new File(filePath);
            BufferedImage image = ImageIO.read(f);
            BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
            HashMap map = new HashMap();
            map.put(EncodeHintType.CHARACTER_SET, "utf-8");
            Result result = reader.decode(bb, map);
            log.info("解析结果：" + result.toString());
            log.info("二维码格式类型：" + result.getBarcodeFormat());
            log.info("二维码文本内容：" + result.getText());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void createImage(String url, String format, OutputStream os) {
        int width = 250;
        int height = 250;
        HashMap map = new HashMap();
        map.put(EncodeHintType.CHARACTER_SET, "utf-8");
        map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        map.put(EncodeHintType.MARGIN, 0);
        try {
            BitMatrix bm = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, width, height);
            MatrixToImageWriter.writeToStream(bm, format, os);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        //create("weixin：//wxpay/bizpayurl?appid=wx2421b1c4370ec43b&mch_id=10000100&nonce_str=f6808210402125e30663234f94c87a8c&product_id=1&time_stamp=1415949957&sign=512F68131DD251DA4A45DA79CC7EFE9D","png","F:\\227126.png");
        //analysis("F:\\227126.png");

    }

    /**
     * 加图片水印
     *
     * @param bufImg  --BufferedImage  用来画图的宽高跟需要加水印的图片一样的空白图
     * @param img     --需要加水印的图片
     * @param markImg --水印图片
     * @param width   --水印图片宽
     * @param height  --水印图片高
     * @param x       --水印相对于底片的x轴坐标(PS:左上角为(0,0))
     * @param y       --水印相对于底片的y轴坐标(PS:左上角为(0,0))
     * @author hwq
     */
    public static void markPic(BufferedImage bufImg, Image img, Image markImg, int width, int height, int x, int y) {
        //取到画笔
        Graphics2D g = bufImg.createGraphics();
        //画底片
        g.drawImage(img, 0, 0, bufImg.getWidth(), bufImg.getHeight(), null);
        //画水印位置
        g.drawImage(markImg, x, y, width, height, null);
        g.dispose();


    }

    /**
     * 加文字水印
     *
     * @param bufImg --BufferedImage  用来画图的宽高跟需要加水印的图片一样的空白图
     * @param img    --需要加水印的图片
     * @param text   --水印文字
     * @param font   --字体
     * @param color  --颜色
     * @param x      --水印相对于底片的x轴坐标(PS:左上角为(0,0))
     * @param y      --水印相对于底片的y轴坐标(PS:左上角为(0,0))
     * @author hwq
     */
    public static void markWord(BufferedImage bufImg, Image img, String text, Font font, Color color, int x, int y) {
        //取到画笔
        Graphics2D g = bufImg.createGraphics();
        //画底片
        g.drawImage(img, 0, 0, bufImg.getWidth(), bufImg.getHeight(), null);
        g.setColor(color);
        g.setFont(font);
        //位置
        g.drawString(text, x / 10, y / 10 + y / 10);
        g.drawString(text, x / 2 - x / 10, y / 2);
        g.drawString(text, x - x / 10 - x / 10, y - y / 10);
        g.dispose();
    }

}